<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,200分)- 服务失效判断（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>某系统中有众多服务&#xff0c;每个服务用字符串&#xff08;只包含字母和数字&#xff0c;长度&lt;&#61;10&#xff09;唯一标识&#xff0c;服务间可能有依赖关系&#xff0c;如A依赖B&#xff0c;则当B故障时导致A也故障。</p> 
<p>依赖具有传递性&#xff0c;如A依赖B&#xff0c;B依赖C&#xff0c;当C故障时导致B故障&#xff0c;也导致A故障。</p> 
<p>给出所有依赖关系&#xff0c;以及当前已知故障服务&#xff0c;要求输出所有正常服务。</p> 
<p>依赖关系&#xff1a;服务1-服务2 表示“服务1”依赖“服务2”</p> 
<p>不必考虑输入异常&#xff0c;用例保证&#xff1a;依赖关系列表、故障列表非空&#xff0c;且依赖关系数&#xff0c;故障服务数都不会超过3000&#xff0c;服务标识格式正常。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>半角逗号分隔的依赖关系列表<strong>&#xff08;换行&#xff09;</strong></p> 
<p>半角逗号分隔的故障服务列表</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>依赖关系列表中提及的所有服务中可以正常工作的服务列表&#xff0c;用半角逗号分隔&#xff0c;按依赖关系列表中出现的次序排序。</p> 
<p>特别的&#xff0c;没有正常节点输出单独一个<strong>半角逗号</strong>。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;"><code>a1-a2,a5-a6,a2-a3</code><br /><code>a5,a2</code></td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">a6,a3</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;"> <p>a1依赖a2&#xff0c;a2依赖a3&#xff0c;所以a2故障&#xff0c;导致a1不可用&#xff0c;但不影响a3&#xff1b;a5故障不影响a6。</p> <p>所以可用的是a3、a6&#xff0c;在依赖关系列表中a6先出现&#xff0c;所以输出:a6,a3。</p> </td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;"><code>a1-a2</code><br /><code>a2</code></td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">,</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">a1依赖a2&#xff0c;a2故障导致a1也故障&#xff0c;没有正常节点&#xff0c;输出一个逗号。</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>我的解题思路是&#xff1a;</p> 
<p>根据第一行输入的依赖关系&#xff0c;统计出每个服务的直接子服务&#xff0c;记录在next中。</p> 
<p>另外由于题目输出描述中说&#xff1a;输出的服务要按照&#xff1a;</p> 
<blockquote> 
 <p>按依赖关系列表中出现的次序排序。</p> 
</blockquote> 
<p>因此&#xff0c;这里我还定义了一个first&#xff0c;用于记录每个服务第一次出现的位置。</p> 
<p></p> 
<p>当上面统计好后&#xff0c;就可以遍历第二行输入的故障服务列表了。</p> 
<p>每遍历到一个故障服务&#xff0c;则删除next中对应服务&#xff0c;但是删除前&#xff0c;需要先记录将要删除服务的所有子服务。删除当前故障服务后&#xff0c;继续递归删除其子服务。</p> 
<p></p> 
<p>这样next剩下的就是正常服务了。</p> 
<p>此时再按照first记录的出现位置对剩余的正常服务排序即可&#xff0c;</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 2) {
    const relatons &#61; lines[0].split(&#34;,&#34;).map((p) &#61;&gt; p.split(&#34;-&#34;)); // 先决条件
    const breakdowns &#61; lines[1].split(&#34;,&#34;); // 故障机器

    console.log(getNormalMachine(relatons, breakdowns));

    lines.length &#61; 0;
  }
});

function getNormalMachine(relatons, breakdowns) {
  const next &#61; {}; // 属性是父服务&#xff0c;属性值是子服务集合
  const first &#61; {}; // 记录服务第一次出现的位置

  let i &#61; 0;
  for (let [c, f] of relatons) {
    if (!next[c]) next[c] &#61; new Set();
    if (!next[f]) next[f] &#61; new Set();
    next[f].add(c);

    if (!first[c]) first[c] &#61; i&#43;&#43;;
    if (!first[f]) first[f] &#61; i&#43;&#43;;
  }

  for (let s of breakdowns) {
    remove(next, s);
  }

  const ans &#61; Object.keys(next);
  if (ans.length &#61;&#61; 0) return &#34;,&#34;;
  return ans.sort((a, b) &#61;&gt; first[a] - first[b]).join(&#34;,&#34;);
}

// 由于服务s是故障服务&#xff0c;因此s服务本身&#xff0c;和其所有子孙服务都无法运行
function remove(next, s) {
  if (next[s]) {
    const need_remove &#61; next[s];
    delete next[s];

    for (let ss of need_remove) {
      remove(next, ss);
    }
  }
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    String[][] relations &#61;
        Arrays.stream(sc.nextLine().split(&#34;,&#34;)).map(s -&gt; s.split(&#34;-&#34;)).toArray(String[][]::new);

    String[] breakdowns &#61; sc.nextLine().split(&#34;,&#34;);

    System.out.println(getResult(relations, breakdowns));
  }

  public static String getResult(String[][] relations, String[] breakdowns) {
    HashMap&lt;String, HashSet&lt;String&gt;&gt; next &#61; new HashMap&lt;&gt;(); // 属性是父服务&#xff0c;属性值是子服务集合
    HashMap&lt;String, Integer&gt; first &#61; new HashMap&lt;&gt;(); // 记录服务第一次出现的位置

    int i &#61; 0;
    for (String[] relation : relations) {
      String c &#61; relation[0];
      String f &#61; relation[1];

      next.putIfAbsent(c, new HashSet&lt;&gt;());
      next.putIfAbsent(f, new HashSet&lt;&gt;());

      next.get(f).add(c);

      first.putIfAbsent(c, i&#43;&#43;);
      first.putIfAbsent(f, i&#43;&#43;);
    }

    for (String s : breakdowns) {
      remove(next, s);
    }

    String[] ans &#61; next.keySet().toArray(new String[0]);
    if (ans.length &#61;&#61; 0) return &#34;,&#34;;

    Arrays.sort(ans, (a, b) -&gt; first.get(a) - first.get(b));
    StringJoiner sj &#61; new StringJoiner(&#34;,&#34;);
    for (String an : ans) sj.add(an);
    return sj.toString();
  }

  // 由于服务s是故障服务&#xff0c;因此s服务本身&#xff0c;和其所有子孙服务都无法运行
  public static void remove(HashMap&lt;String, HashSet&lt;String&gt;&gt; next, String s) {
    if (next.containsKey(s)) {
      HashSet&lt;String&gt; need_remove &#61; next.get(s);
      next.remove(s);

      for (String ss : need_remove) {
        remove(next, ss);
      }
    }
  }
}
</code></pre> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
relations &#61; [s.split(&#34;-&#34;) for s in input().split(&#34;,&#34;)]
breakdowns &#61; input().split(&#34;,&#34;)


def remove(next, s):
    if next.get(s) is not None:
        need_remove &#61; next[s]
        del next[s]

        for ss in need_remove:
            remove(next, ss)


# 算法入口
def getResult():
    next &#61; {}
    first &#61; {}

    i &#61; 0
    for c, f in relations:
        if next.get(c) is None:
            next[c] &#61; set()

        if next.get(f) is None:
            next[f] &#61; set()

        next[f].add(c)

        if first.get(c) is None:
            first[c] &#61; i
            i &#43;&#61; 1

        if first.get(f) is None:
            first[f] &#61; i
            i &#43;&#61; 1

    for s in breakdowns:
        remove(next, s)

    ans &#61; list(next.keys())
    if len(ans) &#61;&#61; 0:
        return &#34;,&#34;

    ans.sort(key&#61;lambda x: first[x])

    return &#34;,&#34;.join(ans)


# 算法调用
print(getResult())
</code></pre> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>